﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SharePointSlideShow.Utility
{
    public class SlideShowLogging:SPDiagnosticsServiceBase
    {
        public static string DiagnosticAreaName = "SharePoint SlideShow Logging";
        public static string CategoryName = "Custom SlideShow Solution";
        public static uint uintEventID = 666;
        private static SlideShowLogging _Current;
        public static SlideShowLogging Current
        {
            get
            { 
                if (_Current == null)
                {
                    _Current = new SlideShowLogging();
                }
                return _Current;
            }
        }
        private SlideShowLogging() : base("Custom SharePoint SlideShow Logging Service", SPFarm.Local) { }
        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
            {
                new SPDiagnosticsArea(DiagnosticAreaName, new List<SPDiagnosticsCategory>
                {
                    new SPDiagnosticsCategory(CategoryName, TraceSeverity.Medium, EventSeverity.Error)
                })
                
            };
            return areas;
        }
        public static string LogErrorInULS(string errorMessage)
        {
            string strExecutionResult = "Message Not Logged in ULS. ";
            try
            {
                SPDiagnosticsCategory category = SlideShowLogging.Current.Areas[DiagnosticAreaName].Categories[CategoryName];
                SlideShowLogging.Current.WriteTrace(uintEventID, category, TraceSeverity.Unexpected, errorMessage);
                strExecutionResult = "Message Logged";
            }
            catch (Exception ex)
            {
                strExecutionResult += ex.Message;
            }
            return strExecutionResult;
        }
        public static string LogErrorInULS(string errorMessage, TraceSeverity tsSeverity)
        {
            string strExecutionResult = "Message Not Logged in ULS. ";
            try
            {
                SPDiagnosticsCategory category = SlideShowLogging.Current.Areas[DiagnosticAreaName].Categories[CategoryName];
                SlideShowLogging.Current.WriteTrace(uintEventID, category, tsSeverity, errorMessage);
                strExecutionResult = "Message Logged";
            }
            catch (Exception ex)
            {
                strExecutionResult += ex.Message;
            }
            return strExecutionResult;
        } 
    }
}




